<?php

namespace App\Http\Controllers\Admin;

use App\Models\Goods;
use App\Models\Picture;
use App\Models\Shop;
use App\Models\ShopGoods;
use App\Models\ShopWaterBillCategory;
use App\Models\WaterBill;

/**
 * 商品管理控制器
 */
class WaterBillController extends BaseController
{

    public function __construct(
        WaterBill $waterBill,
        Goods $goods,
        ShopGoods $shopGoods,
        ShopWaterBillCategory $shopWaterBillCategory,
        Shop $shop,
        Picture $picture
    )
    {
        parent::__construct();
        $this->shopGoods = $shopGoods;
        $this->goods = $goods;
        $this->waterBill = $waterBill;
        $this->shop = $shop;
        $this->shopWaterBillCategory = $shopWaterBillCategory;
        $this->picture = $picture;
    }


    public function getIndex()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $rows = $this->waterBill->getOwnGoodsList();
        if (request('shop_id')) {
            foreach ($shopList as $v) {
                if ($v['id'] == request('shop_id')) {
                    $curentShop = $v;
                }
            }
        }
        $ext = $this->picture->config['saveExt'];
        $mini = 60;
        if ($this->picture->config['cut_Mini_Size']) {
            $miniArr = explode(',', $this->picture->config['cut_Mini_Size']);
            $mini = $miniArr[0];
        }
        return view('admin.bill.index', compact('rows', 'shopList', 'curentShop', 'ext', 'mini'));
    }

    public function getShopIndex()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $rows = $this->shopGoods->getOwnGoodsListWithBill(request('shop_id'));
//        dd($rows);
        if (request('shop_id')) {
            foreach ($shopList as $v) {
                if ($v['id'] == request('shop_id')) {
                    $curentShop = $v;
                }
            }
        }
        $ext = $this->picture->config['saveExt'];
        $mini = 60;
        if ($this->picture->config['cut_Mini_Size']) {
            $miniArr = explode(',', $this->picture->config['cut_Mini_Size']);
            $mini = $miniArr[0];
        }
        return view('admin.bill.shop_index', compact('rows', 'shopList', 'curentShop', 'ext', 'mini'));
    }

    public function getInfo()
    {
        $info = $this->waterBill->_getOne(['goods_id'=>request('id'),'shop_id'=>request('shop_id')]);
        $ext = $this->picture->config['saveExt'];
        $mini = 60;
        if ($this->picture->config['cut_Mini_Size']) {
            $miniArr = explode(',', $this->picture->config['cut_Mini_Size']);
            $mini = $miniArr[0];
        }
        return view('admin.bill.info', compact('info','ext', 'mini'));
    }


    public function getAdd()
    {
        $billCategory = $this->shopWaterBillCategory->getShopBillCategory(request('shop_id'));
        $info = $this->shopGoods->_getOne(['id' => request('id')]);
        $ext = $this->picture->config['saveExt'];
        return view('admin.bill.edit', compact('billCategory', 'info', 'ext'));
    }

    public function postAdd()
    {
        $res = $this->waterBill->addOrUpdate();
        if (!$res) {
            return $this->error($this->waterBill->getError() ? $this->waterBill->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('waterBill', 'getShopIndex', ['shop_id' => request('shop_id'),'page'=>request('page')]));
    }

    public function getEdit()
    {
        $billCategory = $billCategory = $this->shopWaterBillCategory->getShopBillCategory(request('shop_id'));
        $info = $this->waterBill->_getOne(['id' => request('id'),'status'=>1]);
        $info = $info ? $info->toArray() : [];
        $ext = $this->picture->config['saveExt'];
        return view('admin.bill.edit', compact('billCategory', 'info', 'ext'));
    }

    public function postEdit()
    {
        $res = $this->waterBill->edit();
        if (!$res) {
            return $this->error($this->waterBill->getError() ? $this->waterBill->getError() : '未知错误');
        }
        if (request('shop_id')) {
            return $this->success('更新成功', urlAdmin('waterBill', 'getShopIndex', ['shop_id' => request('shop_id')]));
        }
        return $this->success('更新成功', urlAdmin('waterBill', 'getIndex'));
    }

    public function anyChangeStatus()
    {
        $method = request('method');
        $ids = array_unique((array)request('ids'));
        if (empty($ids)) {
            return $this->error('请选择要操作的数据!');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $where['id'] = ['in', $ids];
        $shopId = request('shop_id');
        switch (strtolower($method)) {
            case 'up':
                $res = $this->waterBill->_update($where,['status'=>1]);
                if(!$res) {
                    return $this->error('上架失败!');
                }
                $this->waterBill->_redisGenerateBillListBySid($shopId);
                return $this->success('上架成功!');
            case 'down':
                $res = $this->waterBill->_update($where,['status'=>-1]);
                if(!$res) {
                    return $this->error('下架失败!');
                }
                $this->waterBill->_redisGenerateBillListBySid($shopId);
                return $this->success('下架成功!');
            default:
                return $this->error('参数非法');
        }
    }

    /**
     * 排序
     */
    public function getSort()
    {
        $where['status'] = 1;
        $where['admin_id'] = is_login();
        if (request()->has('ids')) {
            $where['id'] = ['in', [request('ids')]];
        }
        if (request('shop_id')) {
            $where['shop_id'] = intval(request('shop_id'));
        }
        $rows = $this->waterBill->_getList($where, 'id,goods_name', ['sort' => 'asc'])->toArray();
        return view('admin.bill.sort', compact('rows'));
    }

    public function postSort()
    {
        $ids = request('ids');
        $ids = explode(',', $ids);
        $data = [];
        foreach ($ids as $key => $value) {
            $data[] = ['id' => $value, 'sort' => $key + 1];
        }
        $res = $this->waterBill->_updateBatch($data);
        if (!$res) {
            return $this->error('排序失败！');
        }
        $this->waterBill->_redisGenerateBillListBySid(request('shop_id'));
        return $this->success('排序成功！');
    }

}
